using HostServices;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Infrastructure;
using Application;
using TouchSocket.Sockets;
using TouchSocket.Core;
using Application.Plugins;
using Application.Adapter;
using Microsoft.Extensions.Logging;
using Domain.Options;
using System;
using MediatR;
using System.Reflection;
using WebApi.Works;

namespace WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<HeartbeatOptions>(Configuration.GetSection("HeartbeatOptions"));
            services.Configure<ApiConfigOptions>(Configuration.GetSection("ApiConfig"));

            ApiConfigOptions wechatConfiguration = new ApiConfigOptions();
            Configuration.GetSection("ApiConfig").Bind(wechatConfiguration);

            services.AddHttpClient("BaseCallBackUrl", options =>
            {
                options.BaseAddress = new Uri(wechatConfiguration.BaseCallBackUrl);
            });

            services.AddApplication(Configuration);
            services.AddInfrastructure(Configuration);
            services.AddDistributedMemoryCache();
            services.AddSwaggerGen();
            //services.AddHostedService<DeviceClientService>();
            services.AddHostedService<DeviceStateWork>();
            services.AddControllers();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApi v1"));


            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
